package com.ikas.ai.server.module.morningpaper.model.elements;

import com.ikas.ai.server.module.morningpaper.consts.MorningPaperElementPriorities;
import com.ikas.ai.server.module.morningpaper.consts.MorningPaperNormalVariableNames;
import com.ikas.ai.server.module.morningpaper.exception.MorningPaperException;
import com.ikas.ai.server.module.morningpaper.model.dto.MorningPaperVariablesDto;
import com.ikas.ai.server.module.morningpaper.model.po.MorningPaperVariablesPo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.DisposableBean;

import java.math.BigDecimal;
import java.util.Map;

/**
 * @author li.hexian
 * 早报变量顶级父类
 */
@Slf4j
public class MorningPaperTopElement implements DisposableBean {

    /**
     * render方法执行优先级标识
     */
    protected MorningPaperElementPriorities PRIORITY = MorningPaperElementPriorities.FIRST_PRIORITY;

    public MorningPaperElementPriorities getPriority() {
        return PRIORITY;
    }

    /**
     * 当前变量名称
     */
    protected String MORNING_PAPER_VARIABLE_NAME = "";

    private static final String UNIT_CONVERT = "10000";

    public String getMorningPaperVariableName() {
        return MORNING_PAPER_VARIABLE_NAME;
    }

    /**
     * t_morning_paper_variables表一次性读取缓存
     */
    private Map<String, MorningPaperVariablesPo> PO_MAP = null;

    public Map<String, MorningPaperVariablesPo> getPoMap() {
        return PO_MAP;
    }

    /**
     * 渲染mv变量顶级方法
     *
     * @param mv
     * @param transmitMap
     * @return rendered mv
     */
    public MorningPaperVariablesDto render(MorningPaperVariablesDto mv, Map<String, MorningPaperVariablesPo> variablesPoMap, Map<String, Object> transmitMap) {
        this.PO_MAP = variablesPoMap;
        return mv;
    }

    /**
     * 获取变量表变量值
     */
    protected <T> T getValueFromVariableTable(MorningPaperTopElement element, Class<T> clazz) {
        String morningPaperVariableName = element.getMorningPaperVariableName();
        String valueFromTable = PO_MAP.get(morningPaperVariableName).getValue();
        if (MorningPaperNormalVariableNames.thisYearOnGridEnergyOneHundredMillionKwh.name().equals(morningPaperVariableName)) {
            //如果是全年上网电量，该时段上网电量+已经累计
            String currentValue = PO_MAP.get(MorningPaperNormalVariableNames.currentTimeOnGridEnergyTenThousandKwh.name()).getValue();
            if (StringUtils.isNotBlank(currentValue)) {
                double v = new BigDecimal(valueFromTable).add(new BigDecimal(currentValue).divide(new BigDecimal(UNIT_CONVERT))).doubleValue();
                valueFromTable = String.valueOf(v);
            }
        }
        if (MorningPaperNormalVariableNames.thisYearPowerConsumptionOneHundredMillionKwh.name().equals(morningPaperVariableName)) {
            //如果是全年用网电量，该时段用网电量+已经累计
            String currentValue = PO_MAP.get(MorningPaperNormalVariableNames.currentTimePowerConsumptionThousandKwh.name()).getValue();
            if (StringUtils.isNotBlank(currentValue)) {
                double v = new BigDecimal(valueFromTable).add(new BigDecimal(currentValue).divide(new BigDecimal(UNIT_CONVERT))).doubleValue();
                valueFromTable = String.valueOf(v);
            }
        }
        return getT(clazz, valueFromTable);
    }


    protected static <T> T getT(Class<T> clazz, String valueFromTable) {
        if (Double.class.isAssignableFrom(clazz)) {
            return (T) Double.valueOf(valueFromTable);
        } else if (Integer.class.isAssignableFrom(clazz)) {
            return (T) Integer.valueOf(valueFromTable);
        } else if (String.class.isAssignableFrom(clazz)) {
            return (T) String.valueOf(valueFromTable);
        } else if (Long.class.isAssignableFrom(clazz)) {
            return (T) Long.valueOf(valueFromTable);
        }else {
            throw new MorningPaperException(MorningPaperException.parseException);
        }
    }

//    @PostConstruct
//    public void initInfo(){
//        log.info("BEAN CREATED : " +this.getMorningPaperVariableName()+"已创建");
//    }

    @Override
    public void destroy() {
//        log.info("BEAN DESTROYED : "+this.getMorningPaperVariableName()+"已销毁");
    }

}
